programming4us
           
 
 
Windows Phone

Windows Phone 7 : Loading Local Bitmaps from Code

- Free product key for windows 10
- Free Product Key for Microsoft office 365
- Malwarebytes Premium 3.7.1 Serial Keys (LifeTime) 2019
2/10/2011 5:13:07 PM
In a Silverlight program, you’ve seen that a bitmap added to the project as a resource is bound into the executable. It’s so customary to reference that local bitmap directly from XAML that very few experienced Silverlight programmers could tell you offhand how to do it in code. The SilverlightTapToLoad project shows you how.

The SilverlightTapToLoad project contains an Image element in its content grid. The Hello.png bitmap is stored in the Images directory and has a Build Action of Resource.

The MainPage.xaml.cs file requires a using directive for the System.Windows.Media.Imaging namespace for the BitmapImage class. Another using directive for System.Windows.Resources is required for the StreamResourceInfo class.

When the screen is tapped, the event handler accesses the resource using the static GetResourceStream method defined by the Application class:

Example 1. Silverlight Project: SilverlightTapToLoad File: MainPage.xaml.cs
protected override void OnManipulationStarted(ManipulationStartedEventArgs args)
{
Uri uri = new Uri("/SilverlightTapToLoad;component/Images/Hello.png", UriKind
.Relative);
StreamResourceInfo resourceInfo = Application.GetResourceStream(uri);
BitmapImage bmp = new BitmapImage();
bmp.SetSource(resourceInfo.Stream);
img.Source = bmp;

args.Complete();
args.Handled = true;
base.OnManipulationStarted(args);
}


Notice how complicated that URL is! It begins with the name of the program followed by a semicolon, followed by the word “component” and then the folder and filename of the file. If you change the Build Action of the Hello.png file to Content rather than Resource, you can simplify the syntax considerably:

Uri uri = new Uri("Images/Hello.png", UriKind.Relative);

What’s the difference?

Navigate to the Bin/Debug subdirectory of the Visual Studio project and find the SilverlightTapToLoad.xap file that contains your program. If you rename it the file to a ZIP extension you can look inside. The bulk of the file will be SilverlightTapToLoad.dll, the compiled binary.

In both cases, the bitmap is obviously stored somewhere within the XAP file. The difference is this:

  • With a Build Action of Resource for the bitmap, it is stored inside the SilverlightTapToLoad.dll file along with the compiled program

  • With a Build Action of Content, the bitmap is stored external to the SilverlightTapToLoad.dll file but within the XAP file, and when you rename the XAP file to a ZIP file, you can see the Images directory and the file.

Which is better?

In a document entitled “Creating High Performance Silverlight Applications for Windows Phone,” Microsoft has recommending using a Build Action of Content rather than Resource for assets included in your application to minimize the size of the binary and startup time. However, if these assets are in a Silverlight library that the program references, then it is better for them to be embedded in the binary with a Build Action of Resource.

If you have a number of images in your program, and you don’t want to include them all in the XAP file, but you’re nervous about downloading the images, why not do a little of both? Include low resolution (or highly compressed) images in the XAP file, but download better versions asynchronously while the application is running.

Other -----------------
- Windows Phone 7 : Image and ImageSource
- Windows Phone 7 : Images Via the Web
- Windows Phone 7 : Customizing Your E-Mail Signature
- Windows Phone 7 : Managing Mail Folders
- Windows Phone 7: The Silverlight Image Element
- Windows Phone 7: XNA Texture Drawing
- Windows Phone 7: An Introduction to Touch - Routed Events
- Windows Phone 7 : Working with Attachments
- Programming Windows Phone 7: An Introduction to Touch - The Manipulation Events
- Programming Windows Phone 7: An Introduction to Touch - Low-Level Touch Events in Silverlight
- Windows Phone 7: Composing a New Message
- Programming Windows Phone 7: An Introduction to Touch - The XNA Gesture Interface
- Programming Windows Phone 7: An Introduction to Touch - Low-Level Touch Handling in XNA
- Windows Phone 7: Responding to a Message
- Windows Phone 7: Checking for New Messages
- Windows Phone 7: Sorting and Searching Your Mail
- Windows Phone 7: Customizing Your Contacts List
- Windows Phone 7: Working with the Me Card
- Windows Phone 7: Posting to Facebook or Windows Live
- Programming Windows Phone 7 : Simple Clocks (part 2)
 
 
 
Top 10
 
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Finding containers and lists in Visio (part 2) - Wireframes,Legends
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Finding containers and lists in Visio (part 1) - Swimlanes
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Formatting and sizing lists
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Adding shapes to lists
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Sizing containers
- Microsoft Access 2010 : Control Properties and Why to Use Them (part 3) - The Other Properties of a Control
- Microsoft Access 2010 : Control Properties and Why to Use Them (part 2) - The Data Properties of a Control
- Microsoft Access 2010 : Control Properties and Why to Use Them (part 1) - The Format Properties of a Control
- Microsoft Access 2010 : Form Properties and Why Should You Use Them - Working with the Properties Window
- Microsoft Visio 2013 : Using the Organization Chart Wizard with new data
- First look: Apple Watch

- 3 Tips for Maintaining Your Cell Phone Battery (part 1)

- 3 Tips for Maintaining Your Cell Phone Battery (part 2)
programming4us programming4us